package com.iot08.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.iot08.api.annotation.ApiLimit;
import com.iot08.api.config.redis.RedisClusterService;
import com.iot08.api.controller.base.BaseController;
import com.iot08.api.service.AccountService;
import com.iot08.api.utils.SpringContextUtil;
import com.iot08.api.vo.request.UtilCodeVO;
import com.iot08.common.constant.CacheEnum;
import com.iot08.common.utils.VerificationCodeUtil;
import com.iot08.common.utils.response.ResponseEnum;
import com.iot08.common.utils.response.ResponseMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author weichao 1299101858@qq.com
 * @version 1.0
 * @date 2021-10-20 15:56
 */
@Slf4j
@RestController
@RequestMapping("getway/util")
public class UtilController extends BaseController {
    @Resource
    private RedisClusterService redisClusterService;

    @Resource
    private AccountService accountService;

    @Resource
    private VerificationCodeUtil verificationCodeUtil;

    @PostMapping("vcode")
    //60秒内只能请求1次
    @ApiLimit(times_max = 1, timeToLive = 60, suffixKeyFrom = "phone",  limitLevel = ApiLimit.LimitLevel.USER_LEVEL)
    public ResponseMap vcode(@Validated UtilCodeVO vo, BindingResult bindingResult) {
        String identStr = vo.getEmail();
        int tempType = 1;
        String qStr = "email";
        if (vo.getVType() == 0){
            tempType = 0;
            identStr = vo.getPhone();
            qStr = "phone";
        }
        if (identStr == null){
            return ResponseMap.fail("参数错误");
        }
        long count = accountService.count(new Query(Criteria.where(qStr).is(identStr)));
        if(vo.getVScenarioType() != 0 && vo.getVScenarioType() != 3){
            //除了 0:注册 和 3:更换手机(提供的账号信息要没有账号存在) 场景外的, 其他都需要进行账号查找校验
            if(count == 0){
                if (vo.getVType() == 0){
                    return ResponseMap.fail(ResponseEnum.STATUS5011.getCode(), ResponseEnum.STATUS5011.getMsg());
                }else{
                    return ResponseMap.fail(ResponseEnum.STATUS5013.getCode(), ResponseEnum.STATUS5013.getMsg());
                }

            }

        }else{
            if(count > 0){
                if (vo.getVType() == 0){
                    return ResponseMap.fail(ResponseEnum.STATUS5010.getCode(), ResponseEnum.STATUS5010.getMsg());
                }else{
                    return ResponseMap.fail(ResponseEnum.STATUS5012.getCode(), ResponseEnum.STATUS5012.getMsg());
                }
            }
        }
        String oldVcode = redisClusterService.get(CacheEnum.VCODE.getKey()+identStr);
        String verificationCode = verificationCodeUtil.getVerificationCode(identStr, tempType, oldVcode);
        if (verificationCode == null){
            return ResponseMap.fail(ResponseEnum.STATUS403.getCode(), "请求验证码失败");
        }
        redisClusterService.setex(CacheEnum.VCODE.getKey()+identStr, CacheEnum.VCODE.getExpire(), verificationCode);

        JSONObject res = new JSONObject();

        String activeProfile = SpringContextUtil.getActiveProfile();
        if("dev".contains(activeProfile)){
            log.info("测试环境返回验证码,获取验证码类型:vType:{} 获取验证码 vcode:{}", vo.getVType(), verificationCode);
            res.put("vCode", verificationCode);
        }else{
            log.info("获取验证码类型:vType:{} 获取验证码 vcode:{} : vo:{}",vo.getVType(), verificationCode, vo.toString());
        }
        return  ResponseMap.success(res);
    }
}
